// pages/noteEdit/noteEdit.js
wx.cloud.init()
import {
    $toast
} from '../../utils/util'
import request from '../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        noteContent: '',
        noteTitle: '',
        noteType: '',
        noteImg: '',
        useID: '',
        fileList: [],
        show: false,
        actions: [{
                name: '旅行',
            },
            {
                name: '美食',
            },
            {
                name: '电影',
            },
        ],
    },
    showedit(e) {
        console.log(e);
        this.setData({
            noteContent: e.detail.html
        })
    },
    noteTitle(e) {
        console.log(e);
        this.setData({
            noteTitle: e.detail
        })
    },
    publish() {
        let {
            id,
            nickname
        } = wx.getStorageSync('userinfo')
        request('post', 'note/publishNote', {
            noteContent: this.data.noteContent,
            noteTitle: this.data.noteTitle,
            noteImg: this.data.noteImg,
            noteType: this.data.noteType,
            userId: id,
            nickname: nickname
        }).then(res => {
            console.log(123111);
            setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/noteList/noteList',
                },1000)
            })
        })

    },
    onClose() {
        this.setData({
            show: false
        });
    },
    choose() {
        this.setData({
            show: true
        });
    },

    onSelect(event) {
        console.log(event.detail);
        this.setData({
            noteType: event.detail.name
        })
    },
    afterRead(event) {
        const {
            file
        } = event.detail;
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        wx.uploadFile({
            url: 'https://example.weixin.qq.com/upload', // 仅为示例，非真实的接口地址
            filePath: file.url,
            name: 'file',
            formData: {
                user: 'test'
            },
            success(res) {

                // 上传完成需要更新 fileList
                const {
                    fileList = []
                } = this.data;
                fileList.push({
                    ...file,
                    url: res.data
                });
                this.setData({
                    fileList
                });
            },
        });
    },
    noteimg(event) {
        // console.log(event.detail.file.url);
        const {
            file
        } = event.detail
        wx.cloud.uploadFile({
            cloudPath: `${new Date().getTime()}.png`,
            filePath: file.url
        }).then(res => {
            console.log(res)
            let fileList = this.data.fileList
            fileList.push({
                url: res.fileID
            })
            this.setData({
                noteImg: res.fileID,
                fileList: fileList
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})